<template>
  <div class="banner" @mouseenter="closureAutoLoopPlayback" @mouseleave="autoLoopPlayback">
		<span
        class="slide-pre" @click="previousImage">
       <svg class="icon icon-chevron-left" aria-hidden="true">
    <use xlink:href="#icon-left"></use>
      </svg>
    </span>
    <span
        class="slide-next" @click="nextImage">
      <svg class="icon icon-chevron-right" aria-hidden="true">
    <use xlink:href="#icon-right"></use>
      </svg>
		</span>
    <transition-group name="animate__animated animate__bounce" :appear="true" enter-active-class="animate__fadeIn"
                      leave-active-class="animate__fadeOut">
      <div :key="index"
           class="slide"
           v-show="index === curpage"
           v-for="(item,index) in banners">
        <a :href="item.sourceUrl" target="_blank">
          <img class="banner-image" :src="item.imgUrl" alt=""/>
        </a>
      </div>
    </transition-group>
  </div>
</template>

<script>
import '@/assets/font/banner/banner'

export default {
  name: "Carousel",
  data() {
    return {
      banners: [],
      curpage: 0,
      autoLoopPlaybackId: null,
    }
  },
  created() {
    this.getBanners()
  },
  mounted() {
    this.autoLoopPlayback()
  },
  destroyed() {
    this.closureAutoLoopPlayback()
  },
  methods: {
    async getBanners() {
      const {data: res} = await this.$http.get('topbanner')
      this.banners = res.banners
    },
    nextImage() {
      const lastPage = this.banners.length - 1
      if (this.curpage > 0) {
        this.curpage -= 1
      } else {
        this.curpage = lastPage
      }
    },
    previousImage() {
      const lastPage = this.banners.length - 1
      if (this.curpage > 0) {
        this.curpage -= 1
      } else {
        this.curpage = lastPage
      }
    },
    autoLoopPlayback() {//开启自动循环播放轮播图
      this.autoLoopPlaybackId = setInterval(() => {
        this.nextImage()
      }, 3000)
    },
    closureAutoLoopPlayback() {
      if (this.autoLoopPlaybackId) {
        clearInterval(this.autoLoopPlaybackId)
      }
    },
  }
}
</script>

<style scoped lang="less">
.banner {
  position: relative;
  margin-top: 10px;
  width: 100%;
  height: 460px;
  background-color: #f68084;
  
  .slide-pre {
    display: block;
    position: absolute;
    left: 235px;
    top: 50%;
    width: 40px;
    height: 70px;
    margin-top: -35px;
    z-index: 10;
    cursor: pointer;
    
    &:hover {
      background: rgba(0, 0, 0, 0.3);
    }
  }
  
  .slide-next {
    display: block;
    position: absolute;
    right: 0;
    top: 50%;
    width: 40px;
    height: 70px;
    margin-top: -35px;
    z-index: 10;
    cursor: pointer;
    
    &:hover {
      background: rgba(0, 0, 0, 0.3);
    }
  }
  
  .slide {
    position: absolute;
    left: 0;
    top: 0;
    width: 1226px;
    height: 460px;
    transition: all 0.3s;
    
    img {
      width: 1226px;
      height: 460px;
      overflow: hidden;
    }
  }
  
  .icon-chevron-left, .icon-chevron-right {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 30px;
    height: 30px;
    margin-left: -15px;
    margin-top: -15px;
    color: #ecf0f1;
  }
  
  /* 必需 */
  
  .fadeIn-transition {
    transition: all .5s ease;
    opacity: 1;
  }
  
  /* .expand-enter 定义进入的开始状态 */
  /* .expand-leave 定义离开的结束状态 */
  
  .fadeIn-enter, .fadeIn-leave {
    opacity: 0;
  }
}
</style>
